
import React, { Component } from 'react';
import { createPage, base } from 'nc-lightapp-front';
import { initTemplate } from './init';
import { MULTILANG, AREA, PAGECODE, DATASOURCECACHE, FIELDS, URL, UISTATUS } from '../constance';
import { buttonClick, searchBtnClick } from './btnClicks';
import { buttonControl } from './viewControl/buttonControl';
import { initLang } from '../tool/multiLangUtil';
import { createListTitle } from '../tool/titleUtil.js';
const { NCDiv } = base;
const searchId = AREA.searchId;
const tableId = AREA.listTableId;

class DiscountList extends Component {
        constructor(props) {
                super(props);
                props.use.search(searchId);
                props.use.table(tableId);
                initLang(this, MULTILANG.langfileId, MULTILANG.domainName, initTemplate.bind(this, this.props));
        }

        render() {
                const { button, search, table, BillHeadInfo } = this.props;
                const { createButtonApp } = button;
                const { NCCreateSearch } = search;
                const { createSimpleTable } = table;

                return (
                        <div className="nc-bill-list">
                                <NCDiv areaCode={NCDiv.config.HEADER} className="nc-bill-header-area">
                                        <div className="header-title-search-area">
                                                {createListTitle(this)}
                                                {/* 国际化处理：采购折扣*/}
                                        </div>
                                        <div className="header-button-area">
                                                {createButtonApp({
                                                        area: AREA.listHead,
                                                        onButtonClick: buttonClick.bind(this),
                                                })}
                                        </div>
                                </NCDiv>
                                <div className="nc-bill-search-area">
                                        {NCCreateSearch(AREA.searchId, {
                                                dataSource: DATASOURCECACHE.dataSourceListCacheKey,
                                                clickSearchBtn: searchBtnClick.bind(this),
                                                renderCompleteEvent: this.querySchemeAfterEvent,
                                                statusChangeEvent: this.querySchemeAfterEvent,
                                        })}
                                </div>
                                <div className="nc-bill-table-area">
                                        {createSimpleTable(AREA.listTableId, {
                                                dataSource: DATASOURCECACHE.dataSourceListCacheKey,
                                                pkname: FIELDS.pk_head_fields,
                                                showIndex: true, //显示序号
                                                showCheck: true,
                                                onSelected: onSelect.bind(this),
                                                onSelectedAll: onSelect.bind(this),
                                                onRowDoubleClick: (record) => {
                                                        this.props.pushTo(URL.card, {
                                                                status: UISTATUS.browse,
                                                                id: record[FIELDS.pk_head_fields].value,
                                                        });
                                                },
                                                componentInitFinished: () => {
                                                        setTimeout(() => {
                                                                let checkArr = this.props.table.getCheckedRows(AREA.listTableId);
                                                                buttonControl(this.props, checkArr);
                                                        }, 1);
                                                },
                                        })}
                                </div>
                        </div>
                );
        }
}

function onSelect(props, moduleId) {
        buttonControl(props, props.table.getCheckedRows(moduleId));
}

DiscountList = createPage({
        billinfo: {
                billtype: 'grid',
                pagecode: PAGECODE.listPagecode,
                bodycode: {
                        [AREA.listTableId]: 'table',
                },
        },
})(DiscountList);

export default DiscountList;
